/*
** ############################################################################
**     Project   : osdee - Operative Systems Design for Embedded Envrionments
**     File      : Os_Isr.h
**     Revision  ; 1.0
**     Abstract  :
**         Interrup configuration functions definition.
**     Processor : MC9S12XEP100CVL
**     Version   : Component 01.042, Driver 01.05, CPU db: 3.00.036
**     Datasheet : MC9S12XEP100 Rev. 1.19 12/2008
**
**     Copyright : 2014 Fernando Rodriguez, Open source with out any responsability.
**     
**     mail      : frr@gmail.com
** ############################################################################
*/
#ifndef __OS_ISR_H
#define __OS_ISR_H

/** Core modules */
#include "MCU_Cfg.h"
#include <MC9S12XEP100.h> /* I/O map for MC9S12XEP100CVL */
#include "cdef.h"

/* Interrupt Module Initalization */
void Mcu_vInterrupt_Init(void);

void INTERRUPT_ENABLE(void);

void INTERRUPT_DISABLE(void);

	/*
	 * Load the stack pointer for the task, then pull the critical nesting
	 * count and PPAGE register from the stack.  The remains of the
	 * context are restored by the RTI instruction.
	 */
#define RestoreContext()									\
	{																\
		extern volatile void* currentTCB_Stack;						\
																	\
		__asm( "ldx currentTCB_Stack" );								\
		__asm( "movw 0,X,0,SP" );								\
		__asm( "movw 2,X,2,SP" );								\
		__asm( "movw 4,X,4,SP" );								\
		__asm( "movw 6,X,6,SP" );								\
 		__asm( "movw 8,X,8,SP" );								\
		__asm( "movw 10,X,10,SP" );							\
		__asm( "ins"  );                       \
		__asm( "pula" );											\
		__asm( "staa 0x30" ); /* 0x30 = PPAGE */					\
		__asm( "rti" );										\
	}

	/*
	 * By the time this macro is called the processor has already stacked the
	 * registers.  Simply stack the nesting count and PPAGE value, then save
	 * the task stack pointer.
	 */
#define SaveContext()									      	\
	{																            \
		extern volatile void* currentTCB_Stack; 				  \
		extern void Os_vWrapperDispatcherFunction(void); \
		__asm( "ldaa 0x30" );  /* 0x30 = PPAGE */					\
		__asm( "psha" );											\
		__asm( "des"  );                       \
		__asm( "ldx currentTCB_Stack" );								\
		__asm( "movw 0,SP,0,X" );								\
		__asm( "movw 2,SP,2,X" );								\
		__asm( "movw 4,SP,4,X" );								\
		__asm( "movw 6,SP,6,X" );								\
 		__asm( "movw 8,SP,8,X" );								\
		__asm( "movw 10,SP,10,X" );							\
		}
/*
 * By the time this macro is called the processor has already stacked the
 * registers.  Simply stack the nesting count and PPAGE value, then save
 * the task stack pointer.
 */
#define DispatcherService()   \
	{																            \
		extern void Os_vWrapperDispatcherFunction(void); 				    \
							                                                  \
    __asm( "ldx  @Os_vWrapperDispatcherFunction" );							\
  	__asm( "pshx" );                                            \
  	__asm( "rts"  );                                            \
  }

#endif /* __OS_ISR_H */

//    		__asm( "glds 0,vDispatcher" );             \
//UINT16 var=(UINT16)&Os_vWrapperDispatcherFunction;